package com.livegoods.config;


import com.livegoods.message.LivegoodsMessagePublisher;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class SpringCacheConfiguration {


    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory){
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return redisTemplate;
    }


    /**
     * 创建消息发送对象
     */
    @Bean
    public LivegoodsMessagePublisher livegoodsMessagePublisher(AmqpTemplate amqpTemplate){
        LivegoodsMessagePublisher livegoodsMessagePublisher = new LivegoodsMessagePublisher();
        livegoodsMessagePublisher.setAmqpTemplate(amqpTemplate);
        return livegoodsMessagePublisher;
    }

}
